package wang.com;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.Scanner;

/**
 * @ProjectName: untitled
 * @Titile: StudentManagement
 * @Author: 王亦凡1
 * @Description: TODO
 */

    public class StudentManagement {

    private static Scanner scanner = new Scanner(System.in);

    public static void main(String[] args) {
        ArrayList<Student> studentList = new ArrayList<>();
        studentList.add(new Student("1001", "张三", 20, "北京"));
        studentList.add(new Student("1002", "李四", 22, "上海"));
        boolean exit = false;
        while (!exit) {
            displayMenu();
            int choice = getChoice();
            switch (choice) {
                case 1:
                    addStudent(studentList,scanner);
                    break;
                case 2:
                    viewStudents(studentList);
                    break;
                case 3:
                    deleteStudent(studentList,scanner);
                    break;
                case 4:
                    modifyStudent(studentList,scanner);
                    break;
                case 5:
                    exit = true;
                    System.out.println("正在退出系统再见");
                    break;
                default:
                    System.out.println("选择错误请重试");
            }
        }
        scanner.close();
    }

    private static void displayMenu() {
        System.out.println("\n学生管理系统");
        System.out.println("1. 添加学生");
        System.out.println("2. 查看学生");
        System.out.println("3. 删除学生");
        System.out.println("4. 修改学生");
        System.out.println("5. 退出系统");
        System.out.print("Enter your choice: ");
    }

    private static int getChoice() {
        return scanner.nextInt();
    }
    public static void addStudent(ArrayList<Student> list, Scanner scanner) {
        System.out.println("\n--- 添加学生信息 ---");

        System.out.print("请输入学号：");
        String id = scanner.nextLine();

        System.out.print("请输入姓名：");
        String name = scanner.nextLine();

        System.out.print("请输入年龄：");
        int age = scanner.nextInt();
        scanner.nextLine(); // 消耗换行符

        System.out.print("请输入住址：");
        String address = scanner.nextLine();

        // 创建学生对象并赋值
        Student student = new Student(id, name, age, address);

        // 添加到集合
        list.add(student);

        System.out.println("学生 [" + name + "] 添加成功！");
    }

    // 显示所有学生的方法
    public static void displayAllStudents(ArrayList<Student> list) {
        if (list.isEmpty()) {
            System.out.println("当前没有学生信息！");
            return;
        }

        System.out.println("\n--- 所有学生信息 ---");
        for (Student student : list) {
            System.out.println(student);
        }
    }
    public static void viewStudents(ArrayList<Student> list) {
        System.out.println("\n=== 所有学生信息 ===");
        System.out.println("学号\t姓名\t年龄\t住址");
        System.out.println("-----------------------------");

        if (list.isEmpty()) {
            System.out.println("当前没有学生信息！");
            return;
        }

        for (Student student : list) {
            System.out.printf("%s\t%s\t%d岁\t%s\n",
                    student.getId(),
                    student.getName(),
                    student.getAge(),
                    student.getAddress());
        }
    }
    public static void deleteStudent(ArrayList<Student> list, Scanner scanner) {
        if (list.isEmpty()) {
            System.out.println("当前没有学生信息，无法删除！");
            return;
        }

        System.out.print("请输入要删除的学生学号：");
        String id = scanner.nextLine();

        boolean found = false;
        Iterator<Student> iterator = list.iterator();
        while (iterator.hasNext()) {
            Student student = iterator.next();
            if (student.getId().equals(id)) {
                iterator.remove();
                found = true;
                System.out.println("学号为 " + id + " 的学生删除成功！");
                break;
            }
        }

        if (!found) {
            System.out.println("未找到学号为 " + id + " 的学生！");
        }
    }
    public static int getIndex(ArrayList<Student> list, String id) {
        for (int i = 0; i < list.size(); i++) {
            if (list.get(i).getId().equals(id)) {
                return i;
            }
        }
        return -1;
    }

    // 修改学生信息的方法
    public static void modifyStudent(ArrayList<Student> list, Scanner scanner) {
        if (list.isEmpty()) {
            System.out.println("当前没有学生信息，无法修改！");
            return;
        }

        System.out.println("\n--- 修改学生信息 ---");
        System.out.print("请输入要修改的学生学号：");
        String id = scanner.nextLine();

        int index = getIndex(list, id);
        if (index == -1) {
            System.out.println("未找到学号为 " + id + " 的学生！");
            return;
        }

        Student student = list.get(index);
        System.out.println("当前学生信息：" + student);

        System.out.println("\n请输入新的信息（不修改直接回车跳过）：");

        System.out.print("姓名（原：" + student.getName() + "）：");
        String name = scanner.nextLine();
        if (!name.isEmpty()) {
            student.setName(name);
        }

        System.out.print("年龄（原：" + student.getAge() + "）：");
        String ageInput = scanner.nextLine();
        if (!ageInput.isEmpty()) {
            student.setAge(Integer.parseInt(ageInput));
        }

        System.out.print("住址（原：" + student.getAddress() + "）：");
        String address = scanner.nextLine();
        if (!address.isEmpty()) {
            student.setAddress(address);
        }

        list.set(index, student);
        System.out.println("学生信息修改成功！");
        System.out.println("修改后信息：" + student);
    }
}



